<template>
  <div class="splash-screen">
    <div class="splash-content">
      <div class="logo-container">
        <div class="logo-bg">
          <img :src="logoUrl" alt="皮影快闪" class="app-logo">
        </div>
      </div>
      <h1 class="app-name">皮影快闪</h1>
      <div class="loading-container">
        <div class="progress-bar">
          <div class="progress-value" :style="{ width: `${progress}%` }"></div>
        </div>
        <p class="loading-text">
          <span>{{ loadingMessage }}</span>
          <span class="dots"></span>
        </p>
      </div>
      <p class="version-info">版本 {{ appVersion }}</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { getAppVersion, notifySplashReady, onAppReady, notifySplashComplete } from '@utils/ipc'
import logoUrl from '../assets/logo.svg'

// 状态
const progress = ref(0)
const appVersion = ref('1.0.0')
const loadingMessage = ref('正在加载应用')

// 消息列表
const messages = [
  '正在加载应用',
  '正在初始化',
  '正在加载资源',
  '正在准备工作区',
  '即将完成'
]

// 消息定时器
let messageTimer: NodeJS.Timeout | null = null
let progressTimer: number | null = null

// 获取应用版本
async function fetchAppVersion() {
  try {
    appVersion.value = await getAppVersion()
    console.log('获取到应用版本:', appVersion.value)
  } catch (error) {
    console.error('获取应用版本失败:', error)
  }
}

// 更新进度
function startProgressAnimation() {
  const startTime = Date.now()
  const duration = 3000 // 3秒内自动增长到99%
  
  const updateProgress = () => {
    const now = Date.now()
    const elapsed = now - startTime
    progress.value = Math.min(elapsed / duration * 100, 99)
    
    if (progress.value < 99) {
      progressTimer = requestAnimationFrame(updateProgress)
    }
  }
  
  progressTimer = requestAnimationFrame(updateProgress)
}

// 更新消息
function startMessageCycle() {
  let index = 0
  messageTimer = setInterval(() => {
    index = (index + 1) % messages.length
    loadingMessage.value = messages[index]
  }, 1500)
}

// 应用就绪处理
function handleAppReady() {
  console.log('应用准备就绪')
  progress.value = 100
  loadingMessage.value = '准备就绪'
  
  // 清除定时器
  if (messageTimer) clearInterval(messageTimer)
  if (progressTimer) cancelAnimationFrame(progressTimer)
  
  // 短暂延迟后通知主进程启动完成
  setTimeout(() => {
    notifySplashComplete()
  }, 500)
}

// 生命周期钩子
onMounted(async () => {
  console.log('启动界面组件已挂载')
  
  // 获取应用版本
  await fetchAppVersion()
  
  // 开始进度动画和消息循环
  startProgressAnimation()
  startMessageCycle()
  
  // 通知主进程启动界面已准备就绪
  notifySplashReady()
  
  // 监听应用就绪事件
  const removeAppReadyListener = onAppReady(handleAppReady)
  
  // 组件卸载时移除监听器
  onUnmounted(() => {
    removeAppReadyListener()
  })
})
</script>

<style scoped>
.splash-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  background-color: transparent;
}

.splash-content {
  width: 360px;
  text-align: center;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  animation: fadeIn 0.5s ease-out;
}

.logo-container {
  margin-bottom: 20px;
  position: relative;
}

.logo-bg {
  width: 120px;
  height: 120px;
  background-color: #fff;
  border-radius: 30px;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 10px 20px rgba(255, 125, 0, 0.15);
}

.app-logo {
  width: 90px;
  height: 90px;
  object-fit: contain;
  animation: pulse 2s infinite;
}

.app-name {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 24px 0;
  color: #333;
  background: linear-gradient(45deg, #ff7d00, #ff9500);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: 1px;
}

.loading-container {
  margin-bottom: 20px;
}

.progress-bar {
  height: 6px;
  background-color: rgba(0, 0, 0, 0.05);
  border-radius: 10px;
  overflow: hidden;
  position: relative;
  margin: 0 10px;
}

.progress-value {
  height: 100%;
  background: linear-gradient(90deg, #ff7d00, #ff9500);
  border-radius: 10px;
  width: 0%;
  transition: width 0.3s ease;
  box-shadow: 0 0 10px rgba(255, 125, 0, 0.5);
}

.loading-text {
  margin-top: 12px;
  font-size: 14px;
  color: #666;
}

.version-info {
  font-size: 12px;
  color: #999;
  margin: 0;
}

.dots {
  display: inline-block;
  position: relative;
  width: 10px;
  text-align: left;
}

.dots::after {
  content: '';
  animation: dots 1.5s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes dots {
  0% { content: ''; }
  25% { content: '.'; }
  50% { content: '..'; }
  75% { content: '...'; }
  100% { content: ''; }
}
</style> 